package com.ym.controller.patient.findDoctor;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.enums.UserUserTypeEnum;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.core.utils.tkmybatis.TKCriteria;
import com.ym.medical.domain.entity.StandardDeptEntity;
import com.ym.medical.domain.entity.UserEntity;
import com.ym.medical.enums.SysValidEnum;
import com.ym.medical.service.IStandardDeptSV;
import com.ym.spring.SessionContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author daikuangyin
 * @create 2019-04-30 14:39
 **/
@Api(tags = "患者端-科室相关")
@RestController
@ApiSign
@RequestMapping(value = "/patient/find/depart",headers= Constants.Api.VERSION)
public class DepartController extends BaseController {
	private static final Logger LOGGER = LoggerFactory.getLogger(DepartController.class);

	@Autowired
	private IStandardDeptSV standardDeptService;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("查询父级科室列表")
	@GetMapping(value = "/list")
	public APIResponse<PageInfo<StandardDeptEntity>> list(@RequestParam(defaultValue = "1", required = false) int pageNum,
														  @RequestParam(defaultValue = "16", required = false) int pageSize) {
		UserEntity currentUser = SessionContext.getCurrUser();
		TK<StandardDeptEntity> select = TK.select(StandardDeptEntity.class);
		TKCriteria<StandardDeptEntity, Object> where = select.where();
		select.orderBy(StandardDeptEntity::getDeptSort);
		if(UserUserTypeEnum.DOCTOR.getKey().equals(currentUser.getUserType())){
			where.andIn(StandardDeptEntity::getDeptLevel,new Integer[]{0,1})
					.andEqualTo(StandardDeptEntity::getState,"01");

		}else {
			where.andEqualTo(StandardDeptEntity::getDeptLevel,1)
					.andEqualTo(StandardDeptEntity::getState,SysValidEnum.VALID.getKey());
		}
		List<StandardDeptEntity> list = standardDeptService.selectList(select);
		return successPage(list);
	}

	@ApiOperation("根据科室code查询子科室")
	@GetMapping(value = "/list/deptCode")
	public APIResponse<PageInfo<StandardDeptEntity>> listByDeptCode(String deptCode, @RequestParam(defaultValue = "1", required = false) int pageNum, @RequestParam(defaultValue = "16", required = false) int pageSize) {
		PageDomain.startPage(pageNum,pageSize);
		List<StandardDeptEntity> list = standardDeptService.selectList(
				TK.selectWhere(StandardDeptEntity.class)
						.andEqualTo(StandardDeptEntity::getDeptLevel, 2)
						.andEqualTo(StandardDeptEntity::getState, SysValidEnum.VALID.getKey())
						.andEqualTo(StandardDeptEntity::getPartentDeptCode, deptCode)
						.end()
						.orderBy(StandardDeptEntity::getDeptSort)
		);
		return successPage(list);
	}
}
